import {forIn} from 'lodash';

/**
 * 两数相除保留两位小数百分比(不含百分号)
 * @param numerator 分子
 * @param denominator 分母
 * @returns
 */
export const twoDigit = (numerator: number | string, denominator: number | string): string => {
  if (Number(denominator) === 0 || Number.isNaN(Number(denominator))) return '0.00';
  return (Math.round((Number(numerator) / Number(denominator)) * 10000) / 100).toFixed(2);
};

/**
 * ts枚举转select下拉选择项
 * @param enumVal 枚举
 * @returns
 */
export function enumSelector(
  enumVal: Record<string, string | number>,
): Array<{ label: string; value: string | number }> {
  return Object.keys(enumVal)
    .filter((o) => Number.isNaN(Number(o)))
    .map((key) => ({ value: enumVal[key], label: key }));
}

/**
 * ts枚举非数值时反转
 * @param enumVal 枚举
 * @returns
 */
export function enumInversion(enumVal: Record<string, string>): Record<string, string> {
  const obj: Record<string, string> = {};
  forIn(enumVal, (val, key) => {
    obj[val] = key;
  });
  return obj;
}



export function enumInKeyDefault(enumVal: Record<string, string>,key:string | undefined,defaultValue:string): string {
  return key? enumInversion(enumVal)[key] :defaultValue
}

/**
 * ts枚举非数值时反转 取值
 * @param enumVal
 * @param key
 * @return key不存在返回空
 */
export function enumInKey(enumVal: Record<string, string>,key:string | undefined): string {
  return enumInKeyDefault(enumVal,key, '-');
}

export function formatDelay(data: number) {
  if (data === null || data === undefined) {
    return "";
  }

  let t = Number(data);
  if (t < 0) {
    return String(t);
  }

  let hour = 0;
  let minute = 0;

  while (t >= 60 * 60 * 1000) {
    hour++;
    t -= 60 * 60 * 1000;
  }

  while (t >= 60 * 1000) {
    minute++;
    t -= 60 * 1000;
  }

  const result = [];
  if (hour > 0) {
    result.push(hour + " h");
  }
  if (minute > 0) {
    result.push(minute + " m");
  }

  if (t > 0) {
    // JavaScript 的 toFixed(3) 会四舍五入并保留3位小数
    const seconds = (t / 1000).toFixed(3);
    // 移除不必要的零和小数点
    const formattedSeconds = seconds.replace(/\.?0+$/, '');
    result.push(formattedSeconds + " s");
  }

  if (result.length === 0) {
    return "0";
  }
  console.log(result.join(" "));
  return result.join(" ");
}


/**
 * 动态创建隐藏iframe
 * @param url iframe内嵌地址
 */
export function createIFrame(url: string) {
  const iframe = document.createElement('iframe');
  iframe.src = url;
  iframe.style.position = 'absolute';
  iframe.style.width = '0';
  iframe.style.height = '0';
  iframe.style.border = '0';
  iframe.style.visibility = 'hidden';
  document.body.appendChild(iframe);
}

/**
 * 阿拉伯数字转中文数字, 目前兼容到5位数
 * @param {number|string} digit
 */
export function toZhDigit(_digit: number | string) {
  const digit: any = typeof _digit === 'number' ? String(_digit) : _digit;
  const digitLen = digit.length;
  if (digitLen > 5) {
    // eslint-disable-next-line no-console
    console.error('过长');
  }

  const zhNumberList = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  const unit = ['', '十', '百', '千', '万'];

  let result = '';

  for (let d = 0; d < digit.length; d++) {
    const v = digit[d];
    if (v === '0' && d !== digitLen - 1) {
      result = `${result}零`;
    } else {
      result = `${result}${zhNumberList[v]}${unit[digitLen - d - 1]}`;
    }
  }
  if (digitLen === 2) result = result.replace('一十', '十');
  if (digitLen > 2) {
    result = result.replace(/零+/, '零');
  }
  return result[result.length - 1] === '零' ? result.substring(0, result.length - 1) : result;
}

function addZeroToDate(val: number) {
  return val > 9 ? val : `0${val}`;
}

// 计算两个时间差 时间戳转天 dateBegin 开始时间
export function timeToDay(dateBegin: number) {
  //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
  const dateEnd = new Date(); //获取当前时间
  const dateDiff = dateBegin - dateEnd.getTime(); //时间差的毫秒数
  const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
  const leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  const hours = addZeroToDate(Math.floor(leave1 / (3600 * 1000))); //计算出小时数
  //计算相差分钟数
  const leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  const minutes = addZeroToDate(Math.floor(leave2 / (60 * 1000))); //计算相差分钟数
  //计算相差秒数
  const leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  const seconds = addZeroToDate(Math.round(leave3 / 1000));
  let timeFn = '';
  if (dayDiff > 0) {
    timeFn = `${dayDiff}天${hours}:${minutes}:${seconds}`;
  } else {
    timeFn = `${hours}:${minutes}:${seconds}`;
  }
  return timeFn;
}

// 计算版本号
export function compareVersion(v1:any, v2:any) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  const len = Math.max(v1.length, v2.length)

 // 调整两个版本号位数相同
  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  // 循环判断每位数的大小
  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i])
    const num2 = parseInt(v2[i])

    if (num1 >= num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}
